﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JYH.Data;
using Newtonsoft.Json;
using JYH.Model;
using JYH.DataAccess;
using JYH.Framework;

namespace JYH.Business
{

    /// <summary>
    ///  BasPartnerInfo
    /// </summary> 
    public class BasPartnerInfoBLL : BusinessBase<BasPartnerInfo>
    {

        /// <summary>
        ///  BasPartnerInfo
        /// </summary> 
        public BasPartnerInfoBLL()
        {
            myDataBase = DB.D();
            this.SqlSelect = @"; Select BasPartnerInfo.* From BasPartnerInfo";
        }
        /// <summary>
        /// 第三方系统同步到MES系统
        /// </summary>
        /// <param name="ErpLIist">第三方物料信息集合</param>
        /// <param name="Msg"></param>
        /// <returns></returns>
        public static bool SyncPartnerInfo(List<BasPartnerInfo> ErpLIist, out string Msg)
        {

            Msg = string.Empty;
            Database db = DB.D();
            db.BeginTransaction();
            Sql sql = new Sql();
            StringBuilder sb = new StringBuilder();

            List<BasPartnerInfo> MesM = new List<BasPartnerInfo>();
            try
            {

                if (ErpLIist.Count > 0)
                {
                  


                    string NOs = "'',";
                    foreach (BasPartnerInfo m in ErpLIist)
                    {
                        NOs += "'" + m.PartnerNO + "',";
                    }
                    NOs = NOs.Substring(0, NOs.Length - 1);
                    sql = new Sql();
                    sql.Append("Select BasPartnerInfo.* From BasPartnerInfo");
                    sql.Where("BasPartnerInfo.PartnerNO in (" + NOs + ")");
                    sql.OrderBy("BasPartnerInfo.PartnerNO");
                    MesM = db.Fetch<BasPartnerInfo>(sql.SQL);
                    foreach (BasPartnerInfo m in ErpLIist)
                    {
                        int index = MesM.FindIndex(p => p.PartnerNO == m.PartnerNO);
                        if (index > -1)
                        {

                           
                            MesM[index].PartnerName = m.PartnerName;
                            MesM[index].PartnerCode = m.PartnerCode;
                            MesM[index].UpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                            sb.Append(BusinessTool.GetUpdateSql(MesM[index]));
                        }
                        else
                        {
                            BasPartnerInfo obj = new BasPartnerInfo();

                          
                            obj.PartnerNO = m.PartnerNO;
                            obj.PartnerName = m.PartnerName;
                            obj.RType = m.RType;
                            obj.PartnerCode = m.PartnerCode;
                            obj.PartnerID = Guid.NewGuid().ToString();
                            obj.RState = "1";
                            obj.CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                            MesM.Add(obj);
                            sb.Append(BusinessTool.GetInsertSql(obj));
                        }
                    }
                }
                if (sb.Length > 0)
                {
                    db.Execute(sb.ToString());
                }
                db.CompleteTransaction();
                return true;
            }
            catch (Exception e)
            {
                Msg = e.Message;
                db.AbortTransaction();
                return false;
            }


        }
    }

}
